package com.morningstar.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.system.dao.entity.SysPost;
import com.morningstar.system.service.SysPostService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.morningstar.common.constants.permission.PermissionConstants.*;

/**
 * 职位信息接口
 *
 * @author yanglianshan
 */
@Slf4j
@RestController
@RequestMapping("/system/post")
public class SysPostController {

    private static final String PERMISSION = "system.post";
    private static final String PERMISSION_LIST = PERMISSION + LIST;
    private static final String PERMISSION_DETAIL = PERMISSION + DETAIL;
    private static final String PERMISSION_UPDATE = PERMISSION + UPDATE;
    private static final String PERMISSION_ADD = PERMISSION + ADD;
    private static final String PERMISSION_DELETE = PERMISSION + DELETE;

    @Resource
    private SysPostService postService;

    /**
     * 职位信息 列表
     *
     * @param commonQuery 查询条件
     * @return 职位信息列表
     */
    @PostMapping(value = "/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<IPage<SysPost>> list(@RequestBody CommonQuery<SysPost, SysPost> commonQuery) {
        SysPost sysPost = commonQuery.getQuery();
        LambdaQueryWrapper<SysPost> queryWrapper = new LambdaQueryWrapper<>();
        if (sysPost != null) {
            queryWrapper.like(StringUtils.isNotBlank(sysPost.getPostName()), SysPost::getPostName, sysPost.getPostName());
            queryWrapper.like(StringUtils.isNotBlank(sysPost.getPostCode()), SysPost::getPostCode, sysPost.getPostCode());
        }
        queryWrapper.orderByAsc(SysPost::getPostSort);
        return Result.successData(postService.page(commonQuery.getPage(), queryWrapper));
    }

    /**
     * 新增职位
     *
     * @param sysPost 职位实体类
     * @return 基本结果类
     */
    @PostMapping
    @SaCheckPermission(PERMISSION_ADD)
    public Result<Object> add(@RequestBody SysPost sysPost) {
        ServiceCheckResult serviceCheckResult = postService.checkAddAndUpdate(sysPost);
        if (serviceCheckResult.isResult()) {
            return Result.fail(serviceCheckResult.getMessage());
        }
        postService.save(sysPost);
        return Result.successNoData();
    }

    /**
     * 更新职位信息
     *
     * @param sysPost 职位实体类
     * @return 基本结果类
     */
    @PostMapping("/update")
    @SaCheckPermission(PERMISSION_UPDATE)
    public Result<Object> update(@RequestBody SysPost sysPost) {
        ServiceCheckResult serviceCheckResult = postService.checkAddAndUpdate(sysPost);
        if (serviceCheckResult.isResult()) {
            return Result.fail(serviceCheckResult.getMessage());
        }
        postService.updateById(sysPost);
        return Result.successNoData();
    }

    /**
     * 删除职位信息
     *
     * @param postId 职位Id
     * @return 基本结果类
     */
    @GetMapping("/delete")
    @SaCheckPermission(PERMISSION_DELETE)
    public Result<Object> delete(@RequestParam("postId") Long postId) {
        postService.removeById(postId);
        return Result.successNoData();
    }

    /**
     * 职位详细信息
     *
     * @param postId 职位ID
     * @return 基本结果类
     */
    @GetMapping("/detail")
    @SaCheckPermission(PERMISSION_DETAIL)
    public Result<SysPost> detail(@RequestParam("postId") Long postId) {
        return Result.successData(postService.getById(postId));
    }

    /**
     * 职位选择列表
     *
     * @return 职位选择列表
     */
    @GetMapping("/select")
    @SaCheckLogin
    public Result<List<SysPost>> select() {
        LambdaQueryWrapper<SysPost> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysPost::getStatus, "0");
        wrapper.orderByAsc(SysPost::getPostSort);
        wrapper.orderByDesc(SysPost::getCreateTime);
        List<SysPost> list = postService.list(wrapper);
        return Result.successData(list);
    }
}
